package com.yiwei.zxp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.BankCardListModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class BankListAdapter extends ItemAdapter<BankCardListModel.DatasBean, BankListAdapter.DefaultHolder> {


    private final int x8;

    private Context context;
    private double moneyLimit = 0;

    public BankListAdapter(Context context) {
        super(context);
        this.context = context;
        x8 = context.getResources().getDimensionPixelSize(R.dimen.x8);
    }

    public void setMoneyLimit(double moneyLimit) {
        this.moneyLimit = moneyLimit;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_bank_list, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        BankCardListModel.DatasBean item = getItem(position);
        holder.txtName.setText(item.name);
//        Glide.with(context).load(item.icon).into(holder.imgIcon);

        holder.txtBankcard.setText(item.bankcard);
        holder.txtInfo.setText("当月剩余" + item.month_money_limit + "元/当日剩余" + item.day_money_limit + "元");

        Glide.with(context).load(item.icon).into(holder.imgIcon);

        if (item.isCheck) {
            holder.imgDelete.setImageResource(R.mipmap.img_bank_select);
        } else {
            holder.imgDelete.setImageResource(R.mipmap.img_bank_unselect);
        }

        if (moneyLimit > Double.valueOf(item.day_money_limit.replace(",",""))) {
            holder.layoutTotal.setBackgroundResource(R.drawable.btn_grey_radius_8);
            holder.imgDelete.setVisibility(View.GONE);
            holder.txtName.setTextColor(0xff999999);
            holder.txtInfo.setTextColor(0xff999999);
            holder.txtBankcard.setTextColor(0xff999999);
        } else {
            holder.txtName.setTextColor(0xffffffff);
            holder.txtInfo.setTextColor(0xffffffff);
            holder.txtBankcard.setTextColor(0xffffffff);
            holder.imgDelete.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(item.left_color)) {
                item.left_color = "#000000";
            }
            if (TextUtils.isEmpty(item.right_color)) {
                item.right_color = "#000000";
            }
            int colors[] = new int[]{Color.parseColor(item.left_color), Color.parseColor(item.right_color)};
            GradientDrawable g = new GradientDrawable(GradientDrawable.Orientation.TL_BR, colors);
            g.setCornerRadius(x8);
            holder.layoutTotal.setBackgroundDrawable(g);
        }


        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (moneyLimit > Double.valueOf(item.day_money_limit.replace(",",""))) {
                    if (onChooseListener != null) {
                        onChooseListener.onChooseTips(item.day_money_limit);
                    }
                    return;
                }
                for (int i = 0; i < getItemCount(); i++) {
                    if (i == position) {
                        getmSource().get(i).isCheck = true;
                    } else {
                        getmSource().get(i).isCheck = false;
                    }
                    notifyDataSetChanged();
                }
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.id, item.name, item.bankcard, item.icon);
                }
            }
        });
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String id, String name, String bankcard, String icon);

        void onChooseTips(String dayLimit);
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.fl_icon)
        FrameLayout flIcon;
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.txt_bankcard)
        TextView txtBankcard;
        @BindView(R.id.img_delete)
        ImageView imgDelete;
        @BindView(R.id.layout_total)
        RelativeLayout layoutTotal;
        @BindView(R.id.txt_info)
        TextView txtInfo;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
